package com.quant.service;

import com.quant.entity.BacktestResult;
import com.quant.mapper.BacktestResultMapper;
import com.quant.utils.JsonUtils;
import org.springframework.stereotype.Service;

/**
 * @author lulj
 */
@Service
public class BacktestResultService {
	private final BacktestResultMapper resultMapper;

	public BacktestResultService(BacktestResultMapper resultMapper) {
		this.resultMapper = resultMapper;
	}

	public BacktestResult selectByTaskId(Long taskId) {
		return BacktestResultConvert(resultMapper.selectByTaskId(taskId));
	}


	public BacktestResult selectLatest() {
		return BacktestResultConvert(resultMapper.selectLatest());
	}

	private BacktestResult BacktestResultConvert(BacktestResult result) {
		if (result == null) {
			return null;
		}
		result.setNetValueCurve(JsonUtils.parseNetValueCurve(result.getNetValueCurveJson()));
		result.setTradeRecords(JsonUtils.parseTradeRecords(result.getResultJson()));
		return result;
	}
}
